﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace KS.DataWrapper.DataWrapper
{
	public partial class DataCacheObject<T>
	{
		#region One
		private async Task SignalRCollectionUpdateNotify(T item)
		{
			if (this.signalR != null)
			{
				try
				{
					await this.signalR.CollectionUpdate(new List<string>() { item.ID }, typeof(T).Name);
				}
				catch
				{
				}
			}
		}

		private async Task SignalRCollectionAddNotify(T item)
		{
			if (this.signalR != null)
			{
				try
				{
					await this.signalR.CollectionAdd(new List<string>() { item.ID }, typeof(T).Name);
				}
				catch
				{
				}
			}
		}

		private async Task SignalRCollectionRemoveNotify(T item)
		{
			if (this.signalR != null)
			{
				try
				{
					await this.signalR.CollectionRemove(new List<string>() { item.ID }, typeof(T).Name);
				}
				catch
				{
				}
			}
		}
		#endregion

		#region Many
		private async Task SignalRCollectionUpdateNotify(IEnumerable<T> itemList)
		{
			if (this.signalR != null)
			{
				try
				{
					var list = new List<string>();

					foreach (var item in itemList)
					{
						list.Add(item.ID);
					}

					await this.signalR.CollectionUpdate( list, typeof(T).Name);
				}
				catch
				{
				}
			}
		}

		private async Task SignalRCollectionAddNotify(IEnumerable<T> itemList)
		{
			if (this.signalR != null)
			{
				try
				{
					var list = new List<string>();

					foreach (var item in itemList)
					{
						list.Add(item.ID);
					}

					await this.signalR.CollectionAdd(list, typeof(T).Name);
				}
				catch
				{
				}
			}
		}

		private async Task SignalRCollectionRemoveNotify(IEnumerable<T> itemList)
		{
			if (this.signalR != null)
			{
				try
				{
					var list = new List<string>();

					foreach (var item in itemList)
					{
						list.Add(item.ID);
					}

					await this.signalR.CollectionRemove(list, typeof(T).Name);
				}
				catch
				{
				}
			}
		}
		#endregion
	}
}